package com.gavindon.android.adapters

import android.view.FocusFinder
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.constraintlayout.utils.widget.ImageFilterView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gavindon.android.R
import com.gavindon.android.entity.CollectRecordResp
import com.gavindon.android.entity.StudyRecordResp
import com.gavindon.android.network.BASE_URL_IMG
import com.gavindon.android.widget.FocusViewUtils

/**
 * description:
 * Created by liNan on  2022-2-11 11:19
 */
class RecordLeftAdapter(layoutResId: Int, data: MutableList<String>?) :
    BaseQuickAdapter<String, BaseViewHolder>(layoutResId, data) {

    private lateinit var lastFocusView: View
    private var lastFocusIndex = -1

    override fun convert(holder: BaseViewHolder, item: String) {
        holder.setText(R.id.tv_left, item)
        holder.itemView.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                if (lastFocusIndex != holder.absoluteAdapterPosition) {
                    lastFocusView = v
                    val leftWitch =
                        if (data.indexOf(item) == 0) LeftRecordEnum.COLLECT else LeftRecordEnum.STUDY
                    focusChangeListener?.invoke(leftWitch)
                }
                lastFocusIndex = holder.absoluteAdapterPosition
            }
        }

/*        holder.itemView.setOnKeyListener { v, keyCode, event ->
            if (event.action == KeyEvent.ACTION_DOWN) {
                when (keyCode) {
                    KeyEvent.KEYCODE_DPAD_RIGHT -> {
                        dpadListener?.invoke(DirectionEnum.RIGHT)
                        return@setOnKeyListener false
                    }
                    KeyEvent.KEYCODE_DPAD_UP -> {
                        dpadListener?.invoke(DirectionEnum.TOP)
                        return@setOnKeyListener false
                    }
                }
            }
            return@setOnKeyListener false
        }*/
    }

    private var dpadListener: ((DirectionEnum) -> Unit)? = null
    fun setDpadListener(listener: (DirectionEnum) -> Unit) {
        this.dpadListener = listener
    }

    private var focusChangeListener: ((LeftRecordEnum) -> Unit)? = null

    fun setFocusChangeListener(listener: (LeftRecordEnum) -> Unit) {
        this.focusChangeListener = listener
    }
}

/**
 * 收藏记录
 */
class RecordContentAdapter(layoutResId: Int, data: MutableList<CollectRecordResp>?) :
    BaseQuickAdapter<CollectRecordResp, BaseViewHolder>(layoutResId, data) {
    override fun convert(holder: BaseViewHolder, item: CollectRecordResp) {
        holder.setText(R.id.ada_content_tv, item.courseName)
        val imageview = holder.getView<ImageFilterView>(R.id.ada_banner_iv)
        val ivFree = holder.getView<ImageView>(R.id.ada_iv_free)
        Glide.with(holder.itemView).load("$BASE_URL_IMG/${item.courseImageURL}").into(imageview)
        FocusViewUtils.scaleView(holder.itemView, 0)
        ivFree.setBackgroundResource(if (item.isFree == 0) R.mipmap.icon21 else R.mipmap.icon22)

        holder.itemView.setOnKeyListener { v, keyCode, event ->
            if (event.action == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    val finder = FocusFinder.getInstance()
                    val view = finder.findNextFocus(v.parent as ViewGroup, v, View.FOCUS_UP)
                    if (view == null) {
                        dpadTopListener?.invoke()
                        return@setOnKeyListener true
                    }
                }
            }
            return@setOnKeyListener false
        }
    }

    private var dpadTopListener: (() -> Unit)? = null
    fun setDpadTopListener(listener: () -> Unit) {
        dpadTopListener = listener
    }

}


class RecordStudyContentAdapter(layoutResId: Int, data: MutableList<StudyRecordResp>?) :
    BaseQuickAdapter<StudyRecordResp, BaseViewHolder>(layoutResId, data) {
    override fun convert(holder: BaseViewHolder, item: StudyRecordResp) {
        val look = if (item.complete == 1) "已看完" else "观看至${item.percentageOfProgress}%"
        holder.setText(R.id.ada_tv_study_title, item.courseName)
            .setText(R.id.ada_study_time, item.lastDate)
            .setText(R.id.ada_tv_look_percent, look)
        FocusViewUtils.scaleView(holder.itemView, 0)
        holder.itemView.setOnKeyListener { v, keyCode, event ->
            if (event.action == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    val finder = FocusFinder.getInstance()
                    val view = finder.findNextFocus(v.parent as ViewGroup, v, View.FOCUS_UP)
                    if (view == null) {
                        dpadTopListener?.invoke()
                        return@setOnKeyListener true
                    }
                }
            }
            return@setOnKeyListener false
        }
    }

    private var dpadTopListener: (() -> Unit)? = null
    fun setDpadTopListener(listener: () -> Unit) {
        dpadTopListener = listener
    }

}

enum class LeftRecordEnum {
    COLLECT,
    STUDY
}

enum class DirectionEnum() {
    LEFT,
    TOP,
    RIGHT,
    DOWN
}